package cn.sinata.wanglin.neighbor.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.MineCircleAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Circle;
import cn.sinata.wanglin.model.Comment;
import cn.sinata.wanglin.model.Image;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class MineCircleActivity extends RecyclerListActivity {

    private MineCircleAdapter mineCircleAdapter;
    private ArrayList<Circle> mCircles = new ArrayList<>();
    private int pageNo = 0;
    private String userId;
    private String id;

    @Override
    protected void initView() {
        super.initView();
        setTitle("个人圈子");

        id = getIntent().getStringExtra("id");
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);

        setItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .drawable(R.drawable.circle_divider_line)
                .size(2)
                .build());

        showDialog();
        getData();
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        mineCircleAdapter = new MineCircleAdapter(mCircles);
        mineCircleAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(mContext)
                        .go(CircleDetailActivity.class)
                        .put("id",mCircles.get(position).getId())
                        .put("toUser",mCircles.get(position).getAuthorId())
                        .startForResult(0);
            }
        });
        return mineCircleAdapter;
    }

    void getData(){
        HttpManager.getCircleList(id,userId,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<Circle>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<Circle> circles) {
                        if (pageNo == 0) {
                            mCircles.clear();
                        }
                        if (circles.size() == 0) {
                            if (pageNo == 0) {
                                mSwipeRefreshLayout.setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                mSwipeRefreshLayout.setLoadMoreText("没有更多了");
                            }
                        }
                        mCircles.addAll(circles);
                        mineCircleAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 12) {
                if (data != null) {
                    int position = data.getIntExtra("position",0);
                    Comment comment = (Comment) data.getSerializableExtra("data");
                    if (position >= 0 && position < mCircles.size()) {
//                        mCircles.get(position).getCommentList().add(comment);
                        if (mCircles.get(position).getCommentList() == null) {
                            return;
                        }
                        mCircles.get(position).getCommentList().add(comment);
                        int commentNum = mCircles.get(position).getCommentNum();
                        mCircles.get(position).setCommentNum(commentNum+1);
                        mineCircleAdapter.notifyDataSetChanged();
                    }
                }
            }
        }
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }
}
